package com.lbssoft.gpsbd.controller;

import com.alibaba.fastjson.JSON;
import com.lbssoft.datalink.util.Log;
import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.dao.DeviceGeofenceMapper;
import com.lbssoft.gpsbd.dao.GeofenceMapper;
import com.lbssoft.gpsbd.model.Geofence;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.GeofenceDevicesParam;
import com.lbssoft.gpsbd.service.GeofenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api("围栏管理")
@RestController
@RequestMapping(value="/geofences")
@Validated
public class GeofenceController {
	@Autowired
	GeofenceMapper geofenceMapper;

	@Autowired
	DeviceGeofenceMapper deviceGeofenceMapper;

	@Autowired
	GeofenceService geofenceService;


	@ApiOperation(value="添加围栏", notes="添加围栏")
	@RequiresPermissions("user:geofence:add")
	@LogAnnotation(targetType = "geofence",action = "add",remark = "添加围栏")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody Geofence geofence) {
		return geofenceService.add(geofence);
	}
	
	@ApiOperation(value="获取围栏列表", notes="获取用户下所有围栏")
	@RequiresPermissions("user:geofence:view")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list() {
		return geofenceService.list();
	}
	
	@ApiOperation(value="更新围栏", notes="需要同步到采集中心")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="path",name = "id", value = "围栏id", required = true, dataType = "int"),
		@ApiImplicitParam(paramType="body",name = "geofence", value = "围栏信息", required = true, dataType = "Geofence"),
	})
	@RequiresPermissions("user:geofence:update")
	@LogAnnotation(targetType = "geofence",action = "update",remark = "更新围栏")
	@RequestMapping(value = "/{id}",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@NotNull @PathVariable int id,@Validated @RequestBody Geofence geofence) {
		geofence.setId(id);
		Log.info("controller:"+ JSON.toJSONString(geofence));
		return geofenceService.update(geofence);
	}
	

	@ApiOperation(value="围栏绑定设备", notes="根据围栏绑定设备")
	@RequiresPermissions("user:geofence:binddevice")
	@LogAnnotation(targetType = "geofencedevice",action = "update",remark = "围栏绑定设备")
	@RequestMapping(value = "bind",method = {RequestMethod.POST})
	@ResponseBody
	public Result bindDevices(@RequestBody GeofenceDevicesParam param) {
		return geofenceService.bindDevices(param);
	}


	@ApiOperation(value="获取绑定的设备ids", notes="根据围栏id获取绑定的设备ids")
	@RequestMapping(value = "/deviceids/{id}",method = {RequestMethod.GET})
	@ResponseBody
	public Result getDeviceIds(@NotNull @PathVariable long id) {
		return geofenceService.getDeviceIds(id);
	}

	@ApiOperation(value="解除绑定设备", notes="根据围栏解除绑定设备")
	@RequiresPermissions("user:geofence:binddevice")
	@LogAnnotation(targetType = "geofencedevice",action = "update",remark = "解除绑定设备")
	@RequestMapping(value = "unbind",method = {RequestMethod.POST})
	@ResponseBody
	public Result unbindDevices(@RequestBody GeofenceDevicesParam param) {
		return geofenceService.unbindDevices(param);
	}

	@ApiOperation(value="删除围栏", notes="需要同步到采集中心")
	@ApiImplicitParam(paramType="path",name = "id", value = "围栏id", required = true, dataType = "int")
	@RequiresPermissions("user:geofence:delete")
	@LogAnnotation(targetType = "geofencedevice",action = "delete",remark = "删除围栏")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@NotNull @PathVariable long id) {
		return geofenceService.delete(id);
	}
	
}